1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent(
typeof(PhotonView))]
5 public
class CubeExtra : Photon.MonoBehaviour
6 {
7     Vector3 latestCorrectPos = Vector3.zero;
8     Vector3 lastMovement = Vector3.zero;
9     
float lastTime = 0;
10
11     
public void Awake()
12     {
13         
if (photonView.isMine)
14         {
15             
this.enabled = false; // Only enable inter/extrapol for remote players
16         }
17
18         latestCorrectPos = transform.position;
19     }
20
21     
// this method is called by PUN when this script is being "observed" by a PhotonView (setup in inspector)
22     
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
23     {
24         
// Always send transform (depending on reliability of the network view)
25         
if (stream.isWriting)
26         {
27             Vector3 pos = transform.localPosition;
28             Quaternion rot = transform.localRotation;
29             stream.Serialize(
ref pos);
30             stream.Serialize(
ref rot);
31         }
32         
// When receiving, buffer the information
33         
else
34         {
35             
// Receive latest state information
36             Vector3 pos = Vector3.zero;
37             Quaternion rot = Quaternion.identity;
38             stream.Serialize(
ref pos);
39             stream.Serialize(
ref rot);
40
41             lastMovement = (pos - latestCorrectPos) / (Time.time - lastTime);
42
43             lastTime = Time.time;
44             latestCorrectPos = pos;
45
46             transform.position = latestCorrectPos;
47         }
48     }
49
50     
// This only runs where the component is enabled, which is only on remote peers (server/clients)
51     
public void Update()
52     {
53         transform.localPosition += lastMovement * Time.deltaTime;
54     }
55 }


this.enabled = false; Only enable interextrapol for remote players

this method is called by PUN when this script is being "observed" by a PhotonView (setup in inspector)

Always send transform (depending on reliability of the network view)

When receiving, buffer the information

Receive latest state information

This only runs where the component is enabled, which is only on remote peers (serverclients)




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.563 lượt xem

Gõ tìm kiếm nhanh...